<?php
/**
 *  上課日數設定
 * @author cik
 *
 */
class Semester_SemesterDateController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        if ($this->_getParam('yearSemester'))
        $this->view->yearSemester  = $this->_getParam('yearSemester');
        else
        $this->view->yearSemester  = $this->view->Semester['yearSeme'];

        $arr = explode("-", $this->view->yearSemester);



        if ($this->getRequest()->isPost() and $this->getRequest()->getPost('act')) {

            $yearArr = $this->_getParam('year');
            foreach($yearArr as $id=>$val) {
                 $semesterDate = new Semester_Model_SemesterCourseDays();
                 $res = $semesterDate->getTable()->findByDql('year=? AND semester=? AND grade=?',
                  array((int) $arr[0],(int)$arr[1], (int)$id))->getFirst();
                  if ($res) {
                    $res->days = (int)$val;
                    $res->save();
                  }
                  else {
                      $semesterDate->year = (int)$arr[0] ;
                      $semesterDate->semester = (int) $arr[1];
                      $semesterDate->grade = (int) $id;
                      $semesterDate->days = (int)$val;
                      $semesterDate->save();
                  }
            }
        }

         $semesterDate = new Semester_Model_SemesterCourseDays();
        $res = $semesterDate->getTable()->findByDql('year=? AND semester=?', array($arr[0],$arr[1]));

        $resArr = array();
        foreach($res as $row) {
            $resArr[$row->grade] = $row->days;
        }
        $this->view->data = $resArr;

    }



}

